package org.jeecg.modules.wlhy.utils;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.paycenter.enums.UTypeEnum;
import org.jeecg.modules.wlhy.complaint.service.IHyComplaintService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class CodeUtil {

    private static Object complaintLock = new Object();

    @Autowired
    private IHyComplaintService complaintService;

    /**
     * 生成合同号
     * @return
     */
    public static String createContractNum(){

        Date date = new Date();
        String reqDate = DateUtil.format(date, "yyyyMMddHHmmss") + RandomUtil.randomNumbers(4);
        String num = "HT" + reqDate;
        return num;
    }
    /**
     * 生成货源号
     * @return
     */
    public String createOrderNum(){
        Date date = new Date();
        String reqDate = DateUtil.format(date, "yyyyMMddHHmmss") + RandomUtil.randomNumbers(4);
        String num = "HY" + reqDate;
        return num;
    }

    public  String createOrderNum(Date date){
        if(date == null) return StringUtils.EMPTY;
        String reqDate = DateUtil.format(date, "yyyyMMddHHmmss") + RandomUtil.randomNumbers(4);
        String num = "HY" + reqDate;
        return num;
    }

    /**
     * 生成运单号
     * @return
     */
    public String createShippingNum() {
        Date date = new Date();
        String reqDate = DateUtil.format(date, "yyyyMMddHHmmss") + RandomUtil.randomNumbers(4);
        String num = "YD" + reqDate;
        return num;
    }

    public  String createShippingNum(Date date) {
        if(date == null) return StringUtils.EMPTY;
        String reqDate = DateUtil.format(date, "yyyyMMddHHmmss") + RandomUtil.randomNumbers(4);
        String num = "YD" + reqDate;
        return num;
    }

    /**
     * 生成投诉单号
     * @return
     */
    public String createComplaintNum() {
        synchronized(complaintLock) {
            String dateString = DateUtils.getDate("yyyy-MM-dd");
            int count = complaintService.getCountByDate(dateString);
            count++;
            String num = "TS" + dateString.replace("-","") + String.format("%05d",count);
            return num;
        }
    }

    //农行
    public static String getBillNum(){
        Date date = new Date();
        String reqDate = DateUtil.format(date, "yyyyMMddHHmmssSSS");
        reqDate = reqDate + RandomUtil.randomNumbers(10);
        return reqDate;
    }

    public static String getBillNum(UTypeEnum uTypeEnum){
        Date date = new Date();
        String reqDate = DateUtil.format(date, "yyyyMMddHHmmssSSS");
        reqDate = uTypeEnum.uType + reqDate + RandomUtil.randomNumbers(10);
        return reqDate;
    }

    //中信,长度不能超过20位
    public static String getZXBillNum(){
        Date date = new Date();
        String reqDate = DateUtil.format(date, "yyyyMMddHHmmss");
        reqDate = reqDate + RandomUtil.randomNumbers(6);
        return reqDate;
    }

    public static String createNo(String prefix , int len) {
        return prefix + RandomUtil.randomNumbers(len);
    }

    public static String createDateNo(String prefix , int len) {
        Date date = new Date();
        String reqDate = DateUtil.format(date, "yyyyMMddHHmmss") + RandomUtil.randomNumbers(len);
        return prefix + reqDate;
    }

    public  String getBillNum(Date date ){
        if(date == null) return StringUtils.EMPTY;
        String reqDate = DateUtil.format(date, "yyyyMMddHHmmssSSS");
        reqDate = reqDate + RandomUtil.randomNumbers(10);
        return reqDate;
    }

    public static String getInvoiceNum(){
        Date date = new Date();
        String reqDate = DateUtil.format(date, "yyyyMMddHHmmssSSS");
        reqDate = "Y" + reqDate + RandomUtil.randomNumbers(4);
        return reqDate;
    }
    public static String getInvoiceNum(Date date){
        if(date == null) return StringUtils.EMPTY;
        String reqDate = DateUtil.format(date, "yyyyMMddHHmmssSSS");
        reqDate = "Y" + reqDate + RandomUtil.randomNumbers(4);
        return reqDate;
    }

    /**
     * 获取派车单号
     * @param date
     * @return
     */
    public static String createDispatchNum(Date date){
        if(date == null){
            date =  new Date();
        }
        String reqDate = DateUtil.format(date, "yyyyMMddHHmmss") + RandomUtil.randomNumbers(4);
        String num = "PC" + reqDate;
        return num;
    }

    public static String getChannelSeqNoNum(){
        Date date = new Date();
        String reqDate = DateUtil.format(date, "yyyyMMddHHmmssSSS");
        return reqDate + RandomUtil.randomNumbers(10);
    }

    public static String getMerOrderNo(){
        Date date = new Date();
        String reqDate = DateUtil.format(date, "yyyyMMddHHmmssSSS");
        return reqDate + RandomUtil.randomNumbers(10);
    }

    public static String createBizUserId(UTypeEnum uTypeEnum){
        Date date = new Date();
        String reqDate = DateUtil.format(date, "yyyyMMddHHmmssSSS");
        return uTypeEnum.uType + reqDate + RandomUtil.randomNumbers(12);
    }
}
